﻿Imports QLPhuTungOto.Data
Imports QLPhuTungOto.Bus
Imports DevExpress.XtraGrid.Controls

Public Class ucQLDSPhuTung

    Dim db As New QLPhuTungOtoDataContext
    Dim PT_BUS As New PhuTungBUS
    Dim LPT_BUS As New LoaiPhuTungBUS
    Dim NCC_BUS As New NhaCungCapBUS
    Dim X_BUS As New XeBUS
    Dim TrangHienTai As Integer = 1
    Dim TongSoTrang As Integer = 1
    Dim SoDongTrenTrang As Integer = 0

    Private Sub ucQLDSPhuTung_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            UcPhanTrang1.txtSoDongCuaTrang.Text = HamHoTro.DocThongSo("config.config", "PhanTrangPhuTung", "200")
            SoDongTrenTrang = Integer.Parse(UcPhanTrang1.txtSoDongCuaTrang.Text)
            TinhSoTrang()
            LoadDSPhuTung()
            LoadDSLoaiPT()
            LoadDSNhaCC()
            LoadDSXe()

            Dim f As Form = Me.FindForm()
            If f Is Nothing Then
                Return
            End If
            f.KeyPreview = True
            AddHandler f.KeyDown, AddressOf Form_KeyDown

            
        Catch ex As Exception

        End Try
    End Sub

    Public Sub LoadDSPhuTung2()
        gctDSPhuTung.DataSource = PT_BUS.LayBang()
    End Sub

    Public Sub LoadDSPhuTung()
        SoDongTrenTrang = Integer.Parse(Integer.Parse(UcPhanTrang1.txtSoDongCuaTrang.Text))
        gctDSPhuTung.DataSource = PT_BUS.LayBang((TrangHienTai - 1) * SoDongTrenTrang, SoDongTrenTrang)
        UcPhanTrang1.lblTrangHienTai.Text = TrangHienTai.ToString + "/" + TongSoTrang.ToString
    End Sub

    Private Sub LoadDSLoaiPT()
        Dim lstLoaiPhuTung As New List(Of LoaiPhuTung)

        lstLoaiPhuTung = LPT_BUS.LayDSLoaiPhuTung()

        Dim loaipt As New LoaiPhuTung With {.MaLoaiPhuTung = -1,
                                           .TenLoaiPhuTung = "< Thêm mới >..."}

        lstLoaiPhuTung.Add(loaipt)

        cbxLoaiPhuTung.DataSource = lstLoaiPhuTung

        cbxLoaiPhuTung.ValueMember = "MaLoaiPhuTung"
        cbxLoaiPhuTung.DisplayMember = "TenLoaiPhuTung"
    End Sub

    Private Sub LoadDSNhaCC()
        Dim lstNhaCC As New List(Of NhaCungCap)

        lstNhaCC = NCC_BUS.LayDSNhaCungCap()

        Dim nhacc As New NhaCungCap With {.MaNhaCungCap = -1,
                                          .TenNhaCungCap = "< Thêm mới >..."}

        lstNhaCC.Add(nhacc)

        cbxNhaCungCap.DataSource = lstNhaCC
        cbxNhaCungCap.ValueMember = "MaNhaCungCap"
        cbxNhaCungCap.DisplayMember = "TenNhaCungCap"
    End Sub

    Private Sub LoadDSXe()
        Dim lstXe As New List(Of Xe)

        lstXe = X_BUS.LayDSXe()

        Dim x As New Xe With {.MaXe = -1,
                              .TenXe = "< Thêm mới >..."}

        lstXe.Add(x)

        cbxLoaiXe.DataSource = lstXe
        cbxLoaiXe.ValueMember = "MaXe"
        cbxLoaiXe.DisplayMember = "TenXe"
    End Sub

    Private Sub gvDSPhuTung_FocusedRowChanged(ByVal sender As System.Object, ByVal e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs) Handles gvDSPhuTung.FocusedRowChanged
        Try
            txtMaPhuTung.Text = gvDSPhuTung.GetRowCellValue(gvDSPhuTung.FocusedRowHandle, "MaPhuTung")
            cbxLoaiPhuTung.Text = gvDSPhuTung.GetRowCellValue(gvDSPhuTung.FocusedRowHandle, "TenLoaiPhuTung")
            txtTenPhuTung.Text = gvDSPhuTung.GetRowCellValue(gvDSPhuTung.FocusedRowHandle, "TenPhuTung")
            cbxNhaCungCap.Text = gvDSPhuTung.GetRowCellValue(gvDSPhuTung.FocusedRowHandle, "TenNhaCungCap")
            btnHinhAnh.Text = gvDSPhuTung.GetRowCellValue(gvDSPhuTung.FocusedRowHandle, "DuongDan")
            nudSoLuong.Value = gvDSPhuTung.GetRowCellValue(gvDSPhuTung.FocusedRowHandle, "SoLuong")
            txtGiaNhap.Text = gvDSPhuTung.GetRowCellValue(gvDSPhuTung.FocusedRowHandle, "GiaNhap")
            txtGiaBan.Text = gvDSPhuTung.GetRowCellValue(gvDSPhuTung.FocusedRowHandle, "GiaBan")
            dtpNgayNhap.Text = gvDSPhuTung.GetRowCellValue(gvDSPhuTung.FocusedRowHandle, "NgayNhap")
            cbxLoaiXe.Text = gvDSPhuTung.GetRowCellValue(gvDSPhuTung.FocusedRowHandle, "TenXe")
            txtDonVi.Text = gvDSPhuTung.GetRowCellValue(gvDSPhuTung.FocusedRowHandle, "DonVi")
        Catch ex As Exception

        End Try

    End Sub

    Private Sub btnNhapLai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNhapLai.Click
        txtMaPhuTung.Text = ""
        txtTenPhuTung.Text = ""
        nudSoLuong.Value = 0
        txtGiaNhap.Text = ""
        txtGiaBan.Text = ""
        dtpNgayNhap.Value = DateTime.Now
    End Sub

    Private Function KiemTraNhap() As String
        Dim err As String = ""
        err += HamHoTro.KiemTraRong(txtMaPhuTung.Text, "Mã phụ tùng")
        err += HamHoTro.KiemTraRong(txtTenPhuTung.Text, "Tên phụ tùng")

        If btnHinhAnh.Text.Trim.Length = 0 Then
            btnHinhAnh.Text = ".\Images\NoImage.bmp"
        End If

        err += HamHoTro.KiemTraRong(txtGiaNhap.Text, "Giá nhập")
        err += HamHoTro.KiemTraRong(txtGiaBan.Text, "Giá bán")

        err += HamHoTro.KiemTraRong(txtDonVi.Text, "Đơn vị")


        If cbxLoaiPhuTung.Items.Count = 1 Then
            err += "Chưa chọn loại phụ tùng" + ControlChars.NewLine
        End If


        If cbxNhaCungCap.Items.Count = 1 Then
            err += "Chưa chọn nhà cung cấp" + ControlChars.NewLine
        End If


        If cbxLoaiXe.Items.Count = 1 Then
            err += "Chưa chọn loại xe" + ControlChars.NewLine
        End If


        Return err
    End Function

    Private Sub ThemPT()
        Dim PT As New PhuTung With {.MaPhuTung = txtMaPhuTung.Text,
                                         .MaLoaiPhuTung = cbxLoaiPhuTung.SelectedValue,
                                         .TenPhuTung = txtTenPhuTung.Text,
                                         .MaNhaCungCap = cbxNhaCungCap.SelectedValue,
                                         .MaHinhAnh = 1,
                                         .SoLuong = Integer.Parse(nudSoLuong.Value),
                                         .GiaNhap = Double.Parse(txtGiaNhap.Text),
                                         .GiaBan = Double.Parse(txtGiaBan.Text),
                                         .NgayNhap = dtpNgayNhap.Value,
                                         .MaXe = cbxLoaiXe.SelectedValue,
                                         .DonVi = txtDonVi.Text}
        PT_BUS.Them(PT)
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Dim err As String = ""
        err += KiemTraNhap()
        If err = "" Then
            Try
                ThemPT()
                TrangThai.ThayDoiPhuTung()
                LoadDSPhuTung()
            Catch ex As Exception
                MessageBox.Show("Không thể thêm phụ tùng")
            End Try
        Else
            MessageBox.Show(err)
        End If

    End Sub

    Private Sub XoaPT(ByVal selectedIndex As Integer)
        Dim pt As New PhuTung
        pt.MaPhuTung = gvDSPhuTung.GetRowCellValue(selectedIndex, "MaPhuTung")
        PT_BUS.Xoa(pt)
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click

        If MessageBox.Show("Bạn thực sự muốn xóa ?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
            Dim selectedIndex As Integer
            selectedIndex = gvDSPhuTung.FocusedRowHandle
            Try
                XoaPT(selectedIndex)
                TrangThai.ThayDoiPhuTung()
                LoadDSPhuTung()
                gvDSPhuTung.FocusedRowHandle = selectedIndex - 1
            Catch ex As Exception
                MessageBox.Show("Không thể xóa phụ tùng")
            End Try

        End If

    End Sub

    Private Sub CapNhatPT()
        Dim pt As New PhuTung

        pt.MaPhuTung = txtMaPhuTung.Text
        pt.MaLoaiPhuTung = cbxLoaiPhuTung.SelectedValue
        pt.TenPhuTung = txtTenPhuTung.Text
        pt.MaNhaCungCap = cbxNhaCungCap.SelectedValue
        pt.MaHinhAnh = 1
        pt.SoLuong = Integer.Parse(nudSoLuong.Value)
        pt.GiaNhap = Double.Parse(txtGiaNhap.Text)
        pt.GiaBan = Double.Parse(txtGiaBan.Text)
        pt.NgayNhap = dtpNgayNhap.Value
        pt.MaXe = cbxLoaiXe.SelectedValue
        pt.DonVi = txtDonVi.Text

        PT_BUS.Sua(pt)
    End Sub

    Private Sub btnCapNhat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCapNhat.Click
        Dim err As String = ""
        err += KiemTraNhap()
        If err = "" Then
            Try
                CapNhatPT()
                TrangThai.ThayDoiPhuTung()
                LoadDSPhuTung()
            Catch ex As Exception
                MessageBox.Show("Không thể cập nhật thông tin phụ tùng")
            End Try
        Else
            MessageBox.Show(err)
        End If
    End Sub

    Private Sub CapNhatStatus()
        ToolStripStatusLabel1.Text = "Tổng số lượng phụ tùng: " + PT_BUS.DemSoLuongPhuTung().ToString
    End Sub

    Private Sub gvDSPhuTung_DataSourceChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gvDSPhuTung.DataSourceChanged
        Try
            CapNhatStatus()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub txtGiaNhap_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtGiaNhap.KeyPress

        If Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar) Then
        Else
            e.Handled = True
        End If

    End Sub

    Private Sub txtGiaBan_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtGiaBan.KeyPress
        If Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar) Then
        Else
            e.Handled = True
        End If
    End Sub

    Private Sub cbxLoaiPhuTung_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbxLoaiPhuTung.Click

        If cbxLoaiPhuTung.Items.Count = 1 Then
            Dim frm As New frmThemLoaiPhuTung
            frm.ShowDialog()
            LoadDSLoaiPT()
        End If

    End Sub

    Private Sub cbxLoaiPhuTung_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxLoaiPhuTung.SelectedIndexChanged

        If cbxLoaiPhuTung.Items.Count > 1 And cbxLoaiPhuTung.SelectedIndex = cbxLoaiPhuTung.Items.Count - 1 Then
            Dim frm As New frmThemLoaiPhuTung
            frm.ShowDialog()
            LoadDSLoaiPT()
        End If

    End Sub

    Private Sub cbxNhaCungCap_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbxNhaCungCap.Click

        If cbxNhaCungCap.Items.Count = 1 Then
            Dim frm As New frmThemNhaCungCap
            frm.ShowDialog()
            LoadDSNhaCC()
        End If

    End Sub

    Private Sub cbxNhaCungCap_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxNhaCungCap.SelectedIndexChanged

        If cbxNhaCungCap.Items.Count > 1 And cbxNhaCungCap.SelectedIndex = cbxNhaCungCap.Items.Count - 1 Then
            Dim frm As New frmThemNhaCungCap
            frm.ShowDialog()
            LoadDSNhaCC()
        End If

    End Sub

    Private Sub cbxLoaiXe_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbxLoaiXe.Click

        If cbxLoaiXe.Items.Count = 1 Then
            Dim frm As New frmThemXe
            frm.ShowDialog()
            LoadDSXe()
        End If

    End Sub

    Private Sub cbxLoaiXe_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxLoaiXe.SelectedIndexChanged

        If cbxLoaiXe.Items.Count > 1 And cbxLoaiXe.SelectedIndex = cbxLoaiXe.Items.Count - 1 Then
            Dim frm As New frmThemXe
            frm.ShowDialog()
            LoadDSXe()
        End If

    End Sub

    Private Sub UcPhanTrang1_SuaSoDongTrenTrang() Handles UcPhanTrang1.SuaSoDongTrenTrang
        Try
            SoDongTrenTrang = Integer.Parse(UcPhanTrang1.txtSoDongCuaTrang.Text)

            If SoDongTrenTrang > 0 Then
                TinhSoTrang()
                TrangHienTai = 1
                LoadDSPhuTung()
                HamHoTro.GhiThongSo("config.config", "PhanTrangPhuTung", UcPhanTrang1.txtSoDongCuaTrang.Text.Trim)
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub UcPhanTrang1_TrangCuoi_Click() Handles UcPhanTrang1.TrangCuoi_Click
        Try
            TrangHienTai = TongSoTrang
            LoadDSPhuTung()
        Catch ex As Exception

        End Try

    End Sub

    Private Sub UcPhanTrang1_TrangDau_Click() Handles UcPhanTrang1.TrangDau_Click
        Try
            TrangHienTai = 1
            LoadDSPhuTung()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub UcPhanTrang1_TrangSau_Click() Handles UcPhanTrang1.TrangSau_Click
        Try
            If TrangHienTai < TongSoTrang Then
                TrangHienTai += 1
                LoadDSPhuTung()
            End If
        Catch ex As Exception

        End Try


    End Sub

    Private Sub UcPhanTrang1_TrangTruoc_Click() Handles UcPhanTrang1.TrangTruoc_Click
        Try
            If TrangHienTai > 1 Then
                TrangHienTai -= 1
                LoadDSPhuTung()
            End If
        Catch ex As Exception

        End Try


    End Sub

    Private Function TinhSoTrang() As Integer
        Dim st As Integer = 0
        Try

            Dim soluongpt As Integer = PT_BUS.DemSoLuongPhuTung
            st = (soluongpt \ SoDongTrenTrang)

            If soluongpt Mod SoDongTrenTrang <> 0 Then
                st += 1
            End If

            TongSoTrang = st

        Catch ex As Exception

        End Try
        Return st
    End Function


    Dim flag1 As Boolean = False
    Dim isDel As Boolean = False
    Private Sub Form_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
        If TypeOf (Me.ActiveControl) Is FindControl Then
            Dim c As FindControl = Me.ActiveControl
            Dim s As String = c.Controls(0).Controls(5).Text
            If e.KeyCode = Keys.Back Then
                isDel = True
            End If

            If isDel = False Then
                If flag1 = False Then
                    LoadDSPhuTung2()
                    flag1 = True
                End If
            ElseIf isDel = True AndAlso s.Count = 1 Then
                LoadDSPhuTung()
                flag1 = False
                isDel = False
            End If
        End If
    End Sub

    Private Sub label4_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles label4.Paint
        Try

            If TrangThai.ThayDoiPhuTung_PhuTung Then
                TinhSoTrang()
                LoadDSPhuTung()
                TrangThai.ThayDoiPhuTung_PhuTung = False
            End If

            If TrangThai.ThayDoiLoaiPhuTung_PhuTung Then
                LoadDSLoaiPT()
                TrangThai.ThayDoiLoaiPhuTung_PhuTung = False
            End If


            If TrangThai.ThayDoiNhaCungCap_PhuTung Then
                LoadDSNhaCC()
                TrangThai.ThayDoiNhaCungCap_PhuTung = False
            End If


            If TrangThai.ThayDoiXe_PhuTung Then
                LoadDSXe()
                TrangThai.ThayDoiXe_PhuTung = False
            End If

        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnThemTuFileExcel_Click(sender As System.Object, e As System.EventArgs) Handles btnThemTuFileExcel.Click
        GridControl1.DataSource = PT_BUS.LoadDSPhuTung()

        Dim op As New DevExpress.XtraPrinting.XlsExportOptions
        op.ShowGridLines = True
        Dim a As New FolderBrowserDialog

        If a.ShowDialog() = DialogResult.OK Then
            GridControl1.ExportToXls(a.SelectedPath + "/abc.xls".ToString(), op)

        End If

    End Sub
End Class
